package marble.model {

	/**
	 * Conference element, i.e. a lecture,
	 * a report, a poster or a coffee break.
	 */
	[Bindable]
	public class Element {

		public var id:int;
		public var owner_id:int;
		public var place_id:String;
		public var place:Place;

		public var name:String;
		public var description:String;

		public var start_datetime:String;
		public var duration:int;

		public var price:String;
		public var quantity:int;

		public var is_session:Boolean;
		public var is_plan:Boolean;

		/**
		 * Serialize received object to a conference Element.
		 */
		public static function create(obj:Object):Element {
			var elem:Element = new Element();
			for (var field:String in obj) {
				if (field == "place" && obj.place != null) {
					var tmp:Object = obj.place;
					elem.place = new Place(tmp.id, tmp.name, tmp.description);
				} else {
					elem[field] = obj[field];
				}
			}
			return elem;
		}

		/**
		 * Returns the starting time in
		 * minutes elapsed since midnight.
		 */
		public function get timeMinutes():int {
			var date:Date = getDate();
			return date.hours * 60 + date.minutes; 
		}
		
		/**
		 * Returns:
		 * 
		 * 0 - Monday
		 * 1 - Tuesday
		 * ...
		 * 
		 */
		public function getDayOfWeek():int {
			var day:int = getDate().day - 1;	// Date.day is 0 for Sunday
			if (day < 0) day = 6;
			return day;
		}
		
		/**
		 * Parse "start_datetime" string.
		 */
		public function getDate():Date {
			var match:Array = start_datetime.match(/([^-]+)-([^-]+)-([^-]+)\s([^:]+):([^:]+):(.+)/);
			if (match != null && match.length > 3) {
				return new Date(
					match[1],			// year
					int(match[2]) - 1,	// month (zero-based)
					int(match[3]),		// day (1-based)
					int(match[4]),		// hour
					int(match[5]),		// minutes
					int(match[6])		// seconds
				);
			}
			return null;
		}
	}
}